package com.example.demo.controller;

import com.example.demo.entity.UserEntity;
import com.example.demo.exception.BusinessException;
import com.example.demo.response.Response;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "HelloController", description = "user的控制层")
public class HelloController {


    @Autowired
    UserService userService;

    @GetMapping("hello")
    public String hello() {
        return "hello world";
    }


    @GetMapping("getUserById")
    public UserEntity getUserById(int id) {
        return userService.getUserByid(id);
    }

    @GetMapping("getAllUser")
    public List<UserEntity> getAllUser() {
        return userService.getAllUser();
    }


    @GetMapping("/helloException")
    public String helloException() throws Exception {
        throw new Exception("发生错误");
    }

    @GetMapping("/exception")
    public String testException() throws Exception {
        throw new BusinessException("发生错误");
    }

    /**
     * together with response struct
     */
    @GetMapping("/hellos")
    public Response<String> helloTest() {
        return Response.ok().data(userService.hello());
    }
}
